<template>
  <div class="cu-common-layout">
    <div class="cu-common-layout-center">
      <el-row class="cu-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="反馈时间">
              <el-date-picker v-model="query.createTime" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="daterange" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="校区信息">
              <el-select v-model="query.campusId" placeholder="请选择校区" clearable filterable :loading="campusLoading" @focus="loadCampusOptions" :style="{ width: '100%' }">
                <el-option v-for="item in campusOptions" :key="item.id" :label="item.fullName + '(' + item.enCode + ')'" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()" native-type="submit">{{ $t('common.search') }}</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">{{ $t('common.reset') }}</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="cu-common-layout-main cu-flex-main">
        <div class="cu-common-head">
          <div>
            <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()" v-has="'btn_add'">新增</el-button>
            <el-button type="text" icon="el-icon-download" @click="exportData()" v-has="'btn_download'">导出</el-button>
            <el-button type="text" icon="el-icon-delete" @click="handleBatchRemoveDel()" v-has="'btn_batchRemove'">批量删除</el-button>
          </div>
          <div class="cu-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link icon="icon-ym icon-ym-Refresh cu-common-head-icon" :underline="false" @click="reset()" />
            </el-tooltip>
            <screenfull isContainer />
          </div>
        </div>
        <custom-table v-loading="listLoading" :data="list" has-c @selection-change="handleSelectionChange">
          <el-table-column prop="campusCode" label="校区编码" align="center" v-if="custom.hasP('campusCode')" width="80" />
          <el-table-column prop="campusName" label="校区名称" align="left" v-if="custom.hasP('campusName')" width="200" />
          <el-table-column prop="province" label="省份/城市" align="left" v-if="custom.hasP('province')" width="200" />
          <el-table-column prop="content" label="反馈内容" align="left" v-if="custom.hasP('content')" />
          <el-table-column prop="createTime" label="反馈时间" align="left" v-if="custom.hasP('createTime')" width="140" :formatter="custom.tableDateTimeFormat" />
          <el-table-column prop="name" label="反馈人姓名" align="left" v-if="custom.hasP('name')" />
          <!-- <el-table-column prop="mobile" label="反馈人电话" align="left" v-if="custom.hasP('mobile')" />
          <el-table-column prop="accountType" label="帐号类型" align="left" v-if="custom.hasP('accountType')">
            <template slot-scope="scope">
              {{
              scope.row.type | dynamicCodeText(accountOptions)}}
</template>
          </el-table-column> -->
          <!-- <el-table-column prop="isReader" label="是否阅读" align="left" v-if="custom.hasP('isReader')" /> -->
          <el-table-column prop="type" label="反馈类型" align="left" v-if="custom.hasP('type')">
            <template slot-scope="scope">
              {{ scope.row.type | dynamicCodeText(typeOptions) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)" v-has="'btn_edit'">编辑</el-button>
              <el-button type="text" @click="handleDel(scope.row.id)" class="cu-table-delBtn" v-has="'btn_remove'">
                删除
              </el-button>
            </template>
          </el-table-column>
        </custom-table>
        <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />
      </div>
    </div>
    <custom-form v-if="formVisible" ref="CustomForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
  </div>
</template>
<script>
import request from '@/utils/request'
import CustomForm from './Form'
import ExportBox from './ExportBox'
import { getCampusSelector } from '@/api/campus/campus'
export default {
  components: { CustomForm, ExportBox },
  data() {
    return {
      query: {
        createTime: undefined,
        campusId: undefined
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: ''
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [{ prop: 'campusId', label: '校区主键' }, { prop: 'content', label: '反馈内容' }, { prop: 'createTime', label: '反馈时间' }],
      typeOptions: [{ enCode: 'PROBLE', fullName: '使用异常' }, { enCode: 'OPINION', fullName: '意见反馈' }, { enCode: 'OTHERS', fullName: '其他' }],
      campusOptions: [],
      campusLoading: false
    }
  },
  computed: {},
  created() {
    this.initData()
  },
  methods: {
    async loadCampusOptions() {
      try {
        this.campusLoading = true
        const res = await getCampusSelector({ fields: 'id,fullName' })
        this.campusOptions = Array.isArray(res.data) ? res.data : []
      } catch (error) {
        console.error('加载校区数据失败:', error)
        this.$message.error('加载校区数据失败')
        this.campusOptions = []
      } finally {
        this.campusLoading = false
      }
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/feedback`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/campus/feedback/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    handleSelectionChange(val) {
      const res = val.map(item => item.id)
      this.multipleSelection = res
    },
    handleBatchRemoveDel() {
      if (!this.multipleSelection.length) {
        this.$message({
          type: 'error',
          message: '请选择一条数据',
          duration: 1500
        })
        return
      }
      const ids = this.multipleSelection
      this.$confirm('您确定要删除这些数据吗, 是否继续？', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/campus/feedback/batchRemove`,
            method: 'POST',
            data: ids
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail)
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query }
      request({
        url: `/api/campus/feedback/Actions/Export`,
        method: 'GET',
        data: query
      }).then(res => {
        if (!res.data.url) return
        window.location.href = this.define.comUrl + res.data.url
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: ''
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: ''
      }
      this.initData()
    }
  }
}
</script>
